﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Xml.Linq;

public partial class GestionarRegistrarUsuario_RolesUsuario_FormGestionarRolesPermisos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //-------------------------Lineas Agregadas para la seguridad----------------------------------------------------
        if (Session["nit"] != null)
            Response.Redirect("/SUEWebSite/Error.aspx");
        else if (Session["nomUsr"] != null)
        {
            List<String> Permisos = (List<String>)Session["Permisos"];

            if (!Permisos.Contains(System.IO.Path.GetFileName(HttpContext.Current.Request.FilePath)))
                Response.Redirect("/SUEWebSite/Error.aspx");
        }
        else Response.Redirect("/SUEWebSite/Principal.aspx");
        //-------------------------Lineas Agregadas para la seguridad----------------------------------------------------
    }

    protected void CargarListaPermisosRolSeleccionado()
    {
        ListBoxPermisosDelRol.Items.Clear();
        Permiso per = new Permiso();
        List<String> permisos = per.obtPermisos(ListBoxRoles.SelectedValue);
        foreach (String permiso in permisos)
            ListBoxPermisosDelRol.Items.Add(permiso);
    }

    protected void CargarListaDeRoles()
    {
        ListBoxRoles.Items.Clear();
        RolPaginas tmp = new RolPaginas();
        List<RolPaginas> roles = tmp.ObtRolDePaginas();
        foreach (RolPaginas rol in roles)
            ListBoxRoles.Items.Add(rol.obtNombreRol());
    }

    protected void ListBoxRoles_SelectedIndexChanged(object sender, EventArgs e)
    {
        CargarListaPermisosRolSeleccionado();
    }
    protected void ButtonCrearRol_Click(object sender, EventArgs e)
    {
        if (TextBoxNuevoRol.Text != "")
        {
            RolPaginas rol = new RolPaginas(TextBoxNuevoRol.Text);
            rol.GuardarRol();
            CargarListaDeRoles();
        }
    }
    protected void ButtonEliminarRol_Click(object sender, EventArgs e)
    {
        if (ListBoxRoles.SelectedIndex != -1)
        {
            RolPaginas tmp = new RolPaginas(ListBoxRoles.SelectedValue);
            tmp.EliminarRol();
            CargarListaDeRoles();
        }
    }
    protected void ButtonQuitarPermiso_Click(object sender, EventArgs e)
    {
        if (ListBoxPermisosDelRol.SelectedIndex != -1 && ListBoxRoles.SelectedIndex != -1)
        {
            Permiso tmp = new Permiso();
            tmp.eliminarPermiso(ListBoxRoles.SelectedValue, ListBoxPermisosDelRol.SelectedValue);
            CargarListaPermisosRolSeleccionado();
        }
    }
    protected void ButtonDarPermiso_Click(object sender, EventArgs e)
    {
        if (ListBoxRoles.SelectedIndex != -1 && ListBoxPermisos.SelectedIndex != -1)
        {
            Permiso per = new Permiso(ListBoxPermisos.SelectedValue, ListBoxRoles.SelectedValue);
            per.CrearPermiso();
            CargarListaPermisosRolSeleccionado();
        }
    }
}
